<?php
namespace Admin\Model;

class AttributeModel extends CommonModel{
	protected $_fields = ["attr_name","type_id","attr_type","attr_input_type","attr_value"];
	protected $_vaildata = [
		["attr_name","require","属性名称必填"],
		["type_id","require","所属类型必选"],
	];
	public function listDate(){
		$pageSize = 10;
		$count = $this->count();
		$Page = new \Think\Page($count,$pageSize);
		$pageStr = $Page->show();
		$list = $this->order("id")->limit($Page->firstRow.",".$pageSize)->select();
		//将type_id转换为具体的类型名称信息
		/*
		1.链表操作
		2.替换操作
			获取到所有的类型信息
			在将类型信息转换为使用主键id作为索引的数组
			循环具体的数据，在根据type_id进行替换
		*/
		$type = D("type")->select();
		foreach ($type as $key => $value) {
			$typeInfo[$value["id"]] = $value;
		}
		foreach ($list as $key => $value) {
			$list[$key]["type_id"] = $typeInfo[$value["type_id"]]["type_name"];
		}
		return ["pageStr"=>$pageStr,"list"=>$list];
	}
	public function dels($id){
		return $this->where("id=".$id)->delete();
	}
	public function findOneById($id){
		return $this->where("id=".$id)->find();
	}
	public function update($data){
		return $this->sava($data);
	}
}